using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Agriculture.Api.Read.Application.Command;
using Agriculture.ErrorCode;
using MediatR;
using Microsoft.AspNetCore.Mvc;

namespace Agriculture.Api.Read.Controllers
{
    /// <summary>
    /// 问诊单管理
    /// </summary>
    [ApiController]
    [Route("api/[controller]")]
    public class ConsultationOrderController : ControllerBase
    {
        private readonly IMediator _mediator;

        public ConsultationOrderController(IMediator mediator)
        {
            _mediator = mediator;
        }

        /// <summary>
        /// 获取问诊单列表
        /// </summary>
        /// <param name="patientSearch">患者姓名或手机号</param>
        /// <param name="consultationStatus">问诊状态（0：全部，1：审核通过，2：未通过）</param>
        /// <param name="consultationType">问诊方式（0：全部，1：图文问诊，2：视频问诊，3：电话问诊）</param>
        /// <param name="consultationSource">问诊来源（0：全部，1：视频问诊，2：图文问诊，3：电话问诊，4：极速图文，5：极速电话，6：复诊开药）</param>
        /// <param name="startTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <param name="orderStatus">订单状态（0：全部，1：待支付，2：进行中，3：已完成，4：已取消，5：已退诊）</param>
        /// <param name="pageIndex">当前页码</param>
        /// <param name="pageSize">每页大小</param>
        /// <returns>问诊单列表</returns>
        [HttpGet]
        public async Task<ApiPageing<ConsultationOrderDto>> GetConsultationOrders(
            [FromQuery] string patientSearch = null,
            [FromQuery] int? consultationStatus = null,
            [FromQuery] int? consultationType = null,
            [FromQuery] int? consultationSource = null,
            [FromQuery] DateTime? startTime = null,
            [FromQuery] DateTime? endTime = null,
            [FromQuery] int? orderStatus = null,
            [FromQuery] int pageIndex = 1,
            [FromQuery] int pageSize = 10)
        {
            var query = new ConsultationOrderQuery
            {
                PatientSearch = patientSearch,
                ConsultationStatus = consultationStatus,
                ConsultationType = consultationType,
                ConsultationSource = consultationSource,
                StartTime = startTime,
                EndTime = endTime,
                OrderStatus = orderStatus,
                PageIndex = pageIndex,
                PageSize = pageSize
            };

            var result = await _mediator.Send(query);
            
            return new ApiPageing<ConsultationOrderDto>
            {
                Code = ApiEnum.Success,
                Msg = "查询成功",
                TotaCount = result.TotalCount,
                TotaPage = result.TotalPages,
                Data = null,
                PageData = result.ConsultationOrders
            };
        }

        /// <summary>
        /// 获取问诊单详情
        /// </summary>
        /// <param name="orderNo">订单编号</param>
        /// <returns>问诊单详情</returns>
        [HttpGet("{orderNo}")]
        public async Task<ApiResult<ConsultationOrderDetailResult>> GetConsultationOrderDetail(string orderNo)
        {
            var query = new ConsultationOrderDetailQuery
            {
                OrderNo = orderNo
            };

            var result = await _mediator.Send(query);
            
            return new ApiResult<ConsultationOrderDetailResult>
            {
                Code = result == null ? ApiEnum.Fail : ApiEnum.Success,
                Msg = result == null ? "未找到该订单记录" : "查询成功",
                Data = result
            };
        }
    }
} 